﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using FRL.DebtChased.ChaseWorkspace;
using FRL.DebtChased.Silverlight.Library;
using FRL.DebtChased.Silverlight.Library.RichContentService;

namespace FRL.DebtChased.ChildWindows
{

	/// <summary>
	/// Declaration of CancelInstalmentPlanWnd class
	/// Window for stopping the currently active instalment plan
	/// </summary>
	public partial class CancelInstalmentPlanWnd : ChildWindow
	{

		/////////////////////////////////////////////////////////////////
		// Attributes

		private AccountPlanTransport m_accountPlan;

		/////////////////////////////////////////////////////////////////
		// Construction

		public CancelInstalmentPlanWnd()
		{

			// This constructor is provided to help development
			// This constructor should never be called from code

			// Load xaml resource
			InitializeComponent();

		}

		public CancelInstalmentPlanWnd(AccountPlanTransport accountPlan)
		{

			// Initialize member variables
			m_accountPlan = accountPlan;

			// Load xaml resource
			InitializeComponent();

			// Set controls - description
			tbDescription.Text = m_accountPlan.Description;

		}

		/////////////////////////////////////////////////////////////////
		// Event Handlers

		private void OKButton_Click(object sender, RoutedEventArgs e)
		{

			// Check controls
			string strReason = txtReason.Text.Trim();
			if (strReason.Length < 4)
			{

				// Message for user and exit
				MessageBoxes.PopupMessage("The reason must be 4 characters or more.");
				return;

			}

			// Confirm cancelling payment plan
			if (!MessageBoxes.PopupOption("Confirm stopping the active instalment plan?"))
				return;

			// Call server function
			MainPage.Singleton.ShowHourglass(true);
			RichContentServiceClient proxy = new RichContentServiceClient();
			proxy.Endpoint.Address = ExtensionsForSilverlight.CreateDebtChasedEndpoint();
			proxy.KillActiveAccountPlanCompleted += new EventHandler<KillActiveAccountPlanCompletedEventArgs>(proxy_KillActiveAccountPlanCompleted);
			proxy.KillActiveAccountPlanAsync(
				m_accountPlan.AccountPlanId,
				strReason,
				chkAlertManager.IsChecked??false
			);

		}

		void proxy_KillActiveAccountPlanCompleted(object sender, KillActiveAccountPlanCompletedEventArgs e)
		{

			// Show hourglass
			MainPage.Singleton.ShowHourglass(false);

			// Get the result
			if (e.Error != null)
				MessageBoxes.PopupMessage_InternalError(e.Error.Message);
			else if (e.Result != null)
				MessageBoxes.PopupMessage_InternalError(e.Result);
			else
			{

				// Reload the account
				MainPage.Singleton.ChaseScreen.ReloadContextAccount();

				// Close this window
				this.DialogResult = true;

			}

		}

		private void CancelButton_Click(object sender, RoutedEventArgs e)
		{

			// Close window
			this.DialogResult = false;

		}

	}

}

